<?php

/**
 * 案件调解审核
 * @author HelloWorld

 * 2019-8-8
 * UTF-8
 */
class MediateauditController extends Controller_Base
{

    //案件分配记录列表
    public function indexAction()
    {
        $request = $this->getRequest();
        $page = intval($request->getQuery('page', 1));
        $status = intval($request->getQuery('status', 1));
        $limit = intval($request->getQuery('limit', 10));
        $mediationOrganId  = intval($request->getQuery('mediationOrganId', 0));
        $assetsId  = intval($request->getQuery('assetsId', 0));
        $closeReason  = intval($request->getQuery('closeReason', 0));
        $importRecordId = intval($request->getQuery('importRecordId', 0));
        $batshId = $request->getQuery('batshId', '');
        $litigantName = $request->getQuery('litigantName', '');
        $is_show = $request->getQuery('is_show', 0);
        $where = $this->getWhere();
        if($mediationOrganId){
            array_push($where,"mediationOrganId = ".$mediationOrganId);
        }
        if($assetsId){
            array_push($where,"assetsId = ".$assetsId);
        }
        if($importRecordId){
            array_push($where,"importRecordId = ".$importRecordId);
        }
        if($batshId){
            array_push($where,"batshId = '{$batshId}'");
        }
        if($closeReason){
            array_push($where,"closeReason = ".$closeReason);
        }
        if($litigantName){
            $caseId = $this->getCaseIdByLitigantName($litigantName);
            if($caseId){
                array_push($where,"id in ({$caseId})");
            }
        }
        $add_begin_end = trim($this->getRequest()->getQuery('add_begin_end', ''));
        if($add_begin_end){
            $date_tmp =  explode(" 到 ",$add_begin_end);
            $begin_date = $date_tmp[0];
            $end_date = $date_tmp[1];
            if (!empty($begin_date) || !empty($end_date)) {
                $begin_date = $begin_date ? $begin_date . " 00:00:00" : "";
                $end_date = $end_date ? $end_date . " 23:59:59" : "";
            }
            if ($end_date) {
                $end_date = strtotime($end_date);
                array_push($where, "addTime <= {$end_date}");

            }
            if ($begin_date) {
                $begin_date = strtotime($begin_date);
                array_push($where, "addTime >= {$begin_date}");
            }
        }
        $page = $page ? $page : 1;
        $start = ($page - 1) * $limit;
        $query = [
            'mediationOrganId' => $mediationOrganId,
            'assetsId' => $assetsId,
            'importRecordId' => $importRecordId,
            'batshId' => $batshId,
            'litigantName' => $litigantName,
            'add_begin_end' => $add_begin_end,
            'closeReason' => $closeReason,
            'limit' => $limit,
        ];
        $caseStatus = $status == 1 ? 'mediate_audit' : 'close_case';
        array_push($where, "caseStatus = '{$caseStatus}' and sysStatus = 1");
        $seacher = array(
            'where' => $where,
            'limit' => $limit,
            'start' => $start,
            'order' => 'id DESC'
        );
        $cases = Module_Mediate_Case::getInstance()->getAllBySearch(array("*"), $seacher, true);
        $total = Module_Mediate_Case::getInstance()->getAllBySearch(array("count(*) as num"), array('where' => $where), true);
        $total = $total[0]['num'];
        $totalPage = ceil($total / $limit);
        //赋值调解部门
        $this ->getMediationOrgan();
        //赋值资产方
        $this ->getAssets();
        //赋值调解员
        $this ->getMediator();
        //赋值短信
        $this ->getSMSConf('mediateAudit');
        SmartyAdapter::assign('status', $status);
        SmartyAdapter::assign('is_show', $is_show);
        SmartyAdapter::assign('mediateResult', Module_Mediate_Case::$mediateResult);
        SmartyAdapter::assign('repayStatus', Module_Mediate_Case::$repayStatusArr);
        SmartyAdapter::assign('closeReason', Module_Mediate_Case::$closeReason);
        SmartyAdapter::assign('arrList', $cases);
        SmartyAdapter::assign('totalPage', $totalPage);
        SmartyAdapter::assign('totalnum', $total);
        SmartyAdapter::assign('pagesize', $limit);
        SmartyAdapter::assign('curPageNum', $page);
        SmartyAdapter::assign('query', $query);
        SmartyAdapter::assign('page_query', http_build_query($query));
    }

    /**
     * 审核状态,驳回通过
     */
    public function auditAction()
    {
        $request = $this->getRequest();
        $caseId = ($request->getPost('case_id', []));
        $auditStatus = ($request->getPost('audit_status', ''));
        if (empty($caseId) || empty($auditStatus) || !in_array($auditStatus, ['pass', 'reject'])) {
            AjaxTool::returnJsonData(4001);
        }
        if($auditStatus == 'pass'){
            $saveData = [
                'caseStatus' => 'close_case',
                'updateTime' => time()
            ];
        }else{
            $saveData = [
                'caseStatus' => 'case_mediate',
                'mediateStatus' => 'reject_mediate',
                'updateTime' => time()
            ];
        }
        $res = Module_Mediate_Case::getInstance()->saveByIds($saveData, $caseId);
        //修改成功，添加日志记录
        if($res){
            $id_arr = explode(',', $caseId);
            foreach ($id_arr as $id){
                $data = [
                    'caseId' => $id,
                    'status' => 'mediate_audit',
                    'operationUid' => $this->admin['id'],
                    'operationName' => $this->admin['name'],
                    'operationContent' =>"调解审核",
                    'remark' => $auditStatus == 'pass' ? "调解审核通过" : "调解审核驳回",
                    'createTime' => time(),
                ];
                $post_data = [
                    'type' => 'Module_Task_Case',
                    'method' => 'addOperateLog',
                    'data' => $data
                ];
                @Module_Queue_Base::addQueue($post_data);
            }

        }
        AjaxTool::returnJsonData(200, '审核成功' );
    }
    //发送短信
    public function sendSmsAction(){
        $request = $this->getRequest();
        //案件id
        $case_id = $request->getPost('case_id', '');
        $sms_code = $request->getPost('sms_code', '');
        $batch = $request->getPost('batch', 1);
        if (!$case_id) {
            AjaxTool::returnJsonData(4005);
        }
        if($batch == 1){
            $case_id_arr = explode(",", $case_id);
            foreach($case_id_arr as $id){
                $post_data = [
                    'type' => 'Module_Task_Sms',
                    'method' => 'send',
                    'data' => [
                        'caseId'=>$id,
                        'smsCode'=>$sms_code,
                        'operationUid'=>$this->admin['id'],
                        'operationName'=>$this->admin['name'],
                        'mediationOrganId'=>$this->admin['mediationOrganId'],
                    ],
                ];
                @Module_Queue_Base::addQueue($post_data);
            }
            AjaxTool::returnJsonData(200,"提交成功，正在异步发送");

        }else{
            $res = Module_Task_Sms::sendHandler([
                'caseId'=>$case_id,
                'smsCode'=>$sms_code,
                'operationUid'=>$this->admin['id'],
                'operationName'=>$this->admin['name'],
                'mediationOrganId'=>$this->admin['mediationOrganId'],
            ]);
            AjaxTool::returnJsonData($res['code'],$res['message']);
        }

    }

}
